<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="城市名称">
              <el-input v-model="form.keyword" clearable @keyup.enter.native="handleSearch"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建日期" >
              <el-date-picker
                v-model="time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="18">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <router-link to="AddCity">
              <el-button style="margin-left:60px" type="primary">添加</el-button>
              </router-link>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 表格区域 -->
    <div class="listDiv" v-loading="tableLoading">
    <!-- <Tablecomponent> </Tablecomponent> -->
    <el-table ref="tableData" :data="tableData" style="width: 100%" height="500px">
        <!--   -->
        <el-table-column
          fixed
          type="index"
          label="编号"
          width="50"
          align="center"
          :index="table_index"
        />
        <el-table-column label="城市名称" prop="area_name" align="center" width="200px" />
        <el-table-column label="关联省份" prop="high_level_name" align="center">
        </el-table-column>
        <el-table-column label="创建时间" prop="created_at" align="center" width="200px">
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="toRoutes('AddCity',scope.row.id,1,scope.row.area_name)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
  </div>
  </div>
</template>

<script>
/***
 * 1.传入url
 * 
 */
import {region_index} from "@/api/NewRetailSystem"
export default {
  name: "",
  data() {
    return {
      time:[],
      tableData:[],
      tableLoading:false,
      form: {level:3,page:1,size:10},
      page_totals:1,
      page_sizes: [10, 30, 50, 100], // 每页显示个数
    };
  },
  watch: {
    time(val) {
      this.form.start_time = val ? val[0] : [];
      this.form.end_time = val ? val[1] : [];
    }
  },
  mounted(){
    this.getList(this.form)
  },
  methods:{
      // 编号
    table_index(index) {
      return (this.form.page - 1) * this.form.size + index + 1;
    },
       // 路由跳转
    toRoutes(e, id, type,area_name) {
      const route = { name: e, query: { id: id,type:type,area_name:area_name} };
      this.$router.push(route)
    //   const routeUrl = this.$router.resolve(route);
    //   window.open(routeUrl.href, "_blank");
    },
    // 获取列表
    getList(e) {
      this.tableLoading = true;
      region_index(e)
        .then(res => {
          console.log("xinagqing", res);
          if (res.data.code == 200) {
            this.tableData = res.data.data;
            this.page_totals = res.data.meta.total;
            this.tableLoading = false;

          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {
          this.errorStatus(err.msg);
          this.tableLoading = false;
        });
    },
    handleSearch(){
      this.form.page = 1;
      this.getList(this.form);
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.size = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
  }

};
</script>
<style scoped>
/* 主页列表样式 */
.listDiv {
  margin-bottom: 20px;
  padding: 0 20px;
}
.pagesCon {
  text-align: right;
  margin-top: 20px;
}
</style>